<template>
	<view>
		<view class="cu-bar bg-white solid-bottom">
			<view class="action">
				<text class="cuIcon-titles text-blue"></text>{{chapterName}}
			</view>
			<view class="action">
				<button class="cu-btn round line-gray" @click="renewal()">重置</button>
			</view>
		</view>
		
		<!-- 题目信息 -->
		<view>
			<view class="bg-white padding">
				<view class="grid margin-bottom text-center col-5">
					<view class="padding-xs" v-for="(item, index) in questionChapterData" :key="index"
					@click="gotoPractice(index + 1)">
						<view class=" padding round" 
						:class="item.status == 2 ? 'bg-grey': item.status == 0 ? 'bg-red' : 'bg-green' ">
							{{index + 1}}
						</view>
					</view>
					
				</view>
			</view>
		</view>
		<view class="bg-white padding-xl" v-if="questionChapterData.length == 0">
			<u-empty mode="data" text="暂无题目信息"></u-empty>
		</view>
	</view>
</template>

<script>
	import { qbmsQuestionChapterData, qbmsQuestionAnswerRenewal } from "@/api/qbms.js"
	export default {
		data() {
			return {
				chapterId: 0, // 章节ID
				subjectName: "", // 学科名称
				chapterName: "", // 章节名称
				questionChapterData: [], // 题目章节数据
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: options.subjectName
			})
			this.chapterId = options.chapterId;
			this.subjectName = options.subjectName;
			this.chapterName = options.chapterName;
			
		},
		onShow() {
			this.initData();
		},
		methods: {
			/** 初始化数据 */
			initData() {
				// 获取题目章节数据
				this.getQusetionChapterData();
			},
			
			/** 获取题目章节数据 */
			getQusetionChapterData() {
				qbmsQuestionChapterData({
					chapterId: this.chapterId
				}).then(res => {
					this.questionChapterData = res.data;
				})
			},
			
			/** 跳转题目练习 */
			gotoPractice(pageNum) {
				uni.$u.route({
					url: "/pages/qbms/practice",
					params: {
						pageNum: pageNum,
						chapterId: this.chapterId,
						chapterName: this.chapterName,
						subjectName: this.subjectName,
					}
				})
			},
			
			/** 重置练习 */
			renewal() {
				this.$util.$confirm("重做将会清空当前展示题目的答题记录，并同步更新您的错题本。是否确定要重做吗？").then(res => {
					this.confirmRenewal();
				})
			},
			/** 确认重置 */
			confirmRenewal() {
				qbmsQuestionAnswerRenewal({
					chapterId: this.chapterId
				}).then(res => {
					this.getQusetionChapterData();
					uni.showToast({
						title: "重置成功",
						icon: "none"
					})
					
				})
			}
			
			
		}
	}
</script>

